﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SiteWorx_WebParts_Advanced_ProductCatalog_ProductCatalogEditor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadEditor();
    }

    private void LoadEditor()
    {
        string productCatalogWPDataId = Request.QueryString["productCatalogWPDataId"].ToString();

        if (Session[productCatalogWPDataId] != null)
        {
            ProductCatalogWebPartData data = (ProductCatalogWebPartData) Session[productCatalogWPDataId];

            lstProductCatalogs.DataSource = Asah.SiteWorx.ProductCatalog.CatalogList.GetCatalogList();
            lstProductCatalogs.DataBind();

            if (data.ProductCatalogId <= 0)
            {
                lstProductCatalogs.Items.Insert(0, new ListItem("-Select Catalog-", "0"));
            }
           
            lstProductCatalogs.Items.FindByValue(data.ProductCatalogId.ToString()).Selected = true;
            txtRepeatColumns.Text = data.RepeatColumns.ToString();
            txtThumbSize.Text = data.ThumbnailSize.ToString();
            txtMaxThumbHeight.Text = data.MaxThumbnailHeight.ToString();
        }
    }
    
    protected void btnFinish_Click(object sender, EventArgs e)
    {
        string productCatalogWPDataId = Request.QueryString["productCatalogWPDataId"].ToString();

        ((ProductCatalogWebPartData)Session[productCatalogWPDataId]).ProductCatalogId = int.Parse(lstProductCatalogs.SelectedValue);
        ((ProductCatalogWebPartData)Session[productCatalogWPDataId]).RepeatColumns = int.Parse(txtRepeatColumns.Text);
        ((ProductCatalogWebPartData)Session[productCatalogWPDataId]).ThumbnailSize = int.Parse(txtThumbSize.Text);
        ((ProductCatalogWebPartData)Session[productCatalogWPDataId]).MaxThumbnailHeight = int.Parse(txtMaxThumbHeight.Text);

        BuildUpdateScript(productCatalogWPDataId);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string productCatalogWPDataId = Request.QueryString["productCatalogWPDataId"].ToString();
        BuildUpdateScript(productCatalogWPDataId);
    }

    private void BuildUpdateScript(string productCatalogWPDataId)
    {
        string functionName = "Update_" + productCatalogWPDataId.Replace("-", "_");
        string script = "var wnd = getRadWindow(); wnd.BrowserWindow." + functionName + "(wnd); wnd.close();";

        ScriptManager.RegisterStartupScript(this, this.GetType(), "UpdateParentWebPart", script, true);
    }
}
